package com.langhua.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.langhua.common.R;
import com.langhua.entity.NewsTwo;
import com.langhua.service.NewsTwoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/newsTwos")
public class NewsTwoController {


    @Autowired
    private NewsTwoService newsTwoService;


    /**
     * newsTwo信息展示
     * @param current  当前页
     * @param size     每页条数
     * @param newsTwo   条件查询
     * @return
     */
    @GetMapping("{current}/{size}")
    public R<IPage> getPage(@PathVariable Integer current, @PathVariable Integer size, NewsTwo newsTwo){

        IPage page=new Page(current,size);
        LambdaQueryWrapper<NewsTwo> lqw=new LambdaQueryWrapper<NewsTwo>();
        lqw.like(Strings.isNotEmpty(newsTwo.getNewTitle()),NewsTwo::getNewTitle,newsTwo.getNewTitle());

        IPage page1 = newsTwoService.page(page,lqw);
        System.out.println("newsTwo页查询内容："+page1);
        return R.success(page1,"newsTwo分页查询");
    }

    /**
     * newsTwo查询全部信息
     * @return
     */
    @GetMapping("/getAllnewsTwo")
    public R<List<NewsTwo>> getAllnewsTwo(){

        List<NewsTwo> list = newsTwoService.list();
        System.out.println("newsTwo查询全部容："+list);
        return R.success(list,"newsTwo查询全部容");
    }


    /**
     * newsTwo查询推荐内容
     * @return
     */
    @GetMapping("/getByIsRecommendNewsTwo")
    public R<List<NewsTwo>> getByIsRecommendNewsTwo(){

        LambdaQueryWrapper<NewsTwo> lqw= new LambdaQueryWrapper<>();
        lqw.eq(NewsTwo::getIsRecommend,"是");

        List<NewsTwo> list = newsTwoService.list(lqw);
        System.out.println("newsTwo查询推荐内容："+list);
        return R.success(list,"newsTwo查询推荐内容");
    }

    /**
     * 新增新闻2
     * @param newsTwo
     * @return
     */
    @PostMapping("/add")
    public R<String> save(@RequestBody NewsTwo newsTwo){
        log.info("新增新闻2，新闻2信息：{}",newsTwo.toString());

        System.out.println("新增得id："+newsTwo);
        newsTwoService.save(newsTwo);

        return R.success(null,"新增新闻2成功");
    }


    /**
     * 根据id查询新闻2信息 (修改数据回显)
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<NewsTwo> getById(@PathVariable Long id){
        log.info("根据id查询新闻2信息..."+id);
        NewsTwo newsTwo = newsTwoService.getById(id);
        if(newsTwo != null){
            return R.success(newsTwo,null);
        }
        return R.error("没有查询到对应新闻2信息");
    }

    /**
     * 根据id修改新闻2信息()
     * @param newsTwo
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody NewsTwo newsTwo){
        log.info(newsTwo.toString());

        long id = Thread.currentThread().getId();
        log.info("修改id为：{}",id);
        //Long empId = (Long)request.getSession().getAttribute("newsTwo");
        //newsTwo.setUpdateTime(LocalDateTime.now());
        //newsTwo.setUpdateUser(empId);

        newsTwoService.updateById(newsTwo);

        return R.success(null,"新闻2信息修改成功");
    }

    /**
     * 通过id删除newsTwo
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public R<String> deleteById(@PathVariable Long id){
        log.info("删除newsTwo的id为：{}", id);

        newsTwoService.removeById(id);
        return R.success(null,"删除成功！");
    }

}
